#include <iostream>
#include <string>
using namespace std;

//分别利用普通写法和多态技术 实现计算器

//普通写法
class Calculator
{
public:
    int num1;
    int num2;//两个操作数

    int result(string op)
    {
        if (op == "+") 
            return num1 + num2;
        else if (op == "-")
            return num1 - num2;
        else if (op == "*")
            return num1 * num2;
        //如果希望扩展新的功能，需要修改源码
        //在真实的开发中，提倡开闭原则：对拓展进行开发，对修改进行关闭
        
        return 0;
    }
};
void test1()
{
    //创建一个计算器对象
    Calculator c;
    c.num1 = 20;
    c.num2 = 99;

    cout << c.num1 << " + " << c.num2 << " = " << c.result("+") << endl;
    
    cout << c.num1 << " - " << c.num2 << " = " << c.result("-") << endl;
    
    cout << c.num1 << " * " << c.num2 << " = " << c.result("*") << endl;
}


//利用多态来实现计算器
//多态好处：
//1、组织结构清晰
//2、可读性强
//3、利于拓展功能与后期维护

//实现计算器的抽象类
class AbstractCalculator
{
public:
    int num1;
    int num2;

    virtual int result()
    {
        return 0;
    }
};

//加法计算器类
class Add:public AbstractCalculator
{
public:
    int result()
    {
        return num1 + num2;
    };
};

//减法计算器类
class Sub:public AbstractCalculator
{
public:
    int result()
    {
        return num1 - num2;
    };
};

//乘法计算器类
class Mul:public AbstractCalculator
{
public:
    int result()
    {
        return num1 * num2;
    };
};

void test2()
{
    //多态使用条件
    //父类的指针或者引用指向子类对象

    //加法运算
    AbstractCalculator* abc = new Add;
    abc->num1 = 10;
    abc->num2 = 99;

    cout << abc->num1 << " + " << abc->num2 << " = " << abc->result() << endl;
    //用完后，记得销毁
    delete abc;

    //减法运算
    abc = new Sub;
    abc->num1 = 10;
    abc->num2 = 99;

    cout << abc->num1 << " - " << abc->num2 << " = " << abc->result() << endl;
    delete abc;

    //减法运算
    abc = new Mul;
    abc->num1 = 10;
    abc->num2 = 99;

    cout << abc->num1 << " * " << abc->num2 << " = " << abc->result() << endl;
    delete abc;
}

int main()
{
    //test1();
    test2();
    return 0;
}